home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / python2.4 / test / test_hotshot.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2005-10-18  |  5KB  |  133 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.4)
  3.  
  4. import hotshot
  5. import hotshot.log as hotshot
  6. import os
  7. import pprint
  8. import unittest
  9. from test import test_support
  10. from hotshot.log import ENTER, EXIT, LINE
  11.  
  12. def shortfilename(fn):
  13.     if fn:
  14.         return os.path.splitext(os.path.basename(fn))[0]
  15.     else:
  16.         return fn
  17.  
  18.  
  19. class UnlinkingLogReader(hotshot.log.LogReader):
  20.     """Extend the LogReader so the log file is unlinked when we're
  21.     done with it."""
  22.     
  23.     def __init__(self, logfn):
  24.         self._UnlinkingLogReader__logfn = logfn
  25.         hotshot.log.LogReader.__init__(self, logfn)
  26.  
  27.     
  28.     def next(self, index = None):
  29.         
  30.         try:
  31.             return hotshot.log.LogReader.next(self)
  32.         except StopIteration:
  33.             self.close()
  34.             os.unlink(self._UnlinkingLogReader__logfn)
  35.             raise 
  36.  
  37.  
  38.  
  39.  
  40. class HotShotTestCase(unittest.TestCase):
  41.     
  42.     def new_profiler(self, lineevents = 0, linetimings = 1):
  43.         self.logfn = test_support.TESTFN
  44.         return hotshot.Profile(self.logfn, lineevents, linetimings)
  45.  
  46.     
  47.     def get_logreader(self):
  48.         return UnlinkingLogReader(self.logfn)
  49.  
  50.     
  51.     def get_events_wotime(self):
  52.         L = []
  53.         for event in self.get_logreader():
  54.             (filename, lineno, funcname) = (what,)
  55.             tdelta = event
  56.             L.append((what, (shortfilename(filename), lineno, funcname)))
  57.         
  58.         return L
  59.  
  60.     
  61.     def check_events(self, expected):
  62.         events = self.get_events_wotime()
  63.         if events != expected:
  64.             self.fail('events did not match expectation; got:\n%s\nexpected:\n%s' % (pprint.pformat(events), pprint.pformat(expected)))
  65.         
  66.  
  67.     
  68.     def run_test(self, callable, events, profiler = None):
  69.         if profiler is None:
  70.             profiler = self.new_profiler()
  71.         
  72.         self.failUnless(not (profiler._prof.closed))
  73.         profiler.runcall(callable)
  74.         self.failUnless(not (profiler._prof.closed))
  75.         profiler.close()
  76.         self.failUnless(profiler._prof.closed)
  77.         self.check_events(events)
  78.  
  79.     
  80.     def test_addinfo(self):
  81.         
  82.         def f(p):
  83.             p.addinfo('test-key', 'test-value')
  84.  
  85.         profiler = self.new_profiler()
  86.         profiler.runcall(f, profiler)
  87.         profiler.close()
  88.         log = self.get_logreader()
  89.         info = log._info
  90.         list(log)
  91.         self.failUnless(info['test-key'] == [
  92.             'test-value'])
  93.  
  94.     
  95.     def test_line_numbers(self):
  96.         
  97.         def f():
  98.             y = 2
  99.             x = 1
  100.  
  101.         
  102.         def g():
  103.             f()
  104.  
  105.         f_lineno = f.func_code.co_firstlineno
  106.         g_lineno = g.func_code.co_firstlineno
  107.         events = [
  108.             (ENTER, ('test_hotshot', g_lineno, 'g')),
  109.             (LINE, ('test_hotshot', g_lineno + 1, 'g')),
  110.             (ENTER, ('test_hotshot', f_lineno, 'f')),
  111.             (LINE, ('test_hotshot', f_lineno + 1, 'f')),
  112.             (LINE, ('test_hotshot', f_lineno + 2, 'f')),
  113.             (EXIT, ('test_hotshot', f_lineno, 'f')),
  114.             (EXIT, ('test_hotshot', g_lineno, 'g'))]
  115.         self.run_test(g, events, self.new_profiler(lineevents = 1))
  116.  
  117.     
  118.     def test_start_stop(self):
  119.         profiler = self.new_profiler()
  120.         profiler.start()
  121.         profiler.stop()
  122.         profiler.close()
  123.         os.unlink(self.logfn)
  124.  
  125.  
  126.  
  127. def test_main():
  128.     test_support.run_unittest(HotShotTestCase)
  129.  
  130. if __name__ == '__main__':
  131.     test_main()
  132.  
  133.